package com.cslg.ac.demo.service.user;

import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.pet.LongAndLatDAO;
import com.cslg.ac.demo.dao.user.po.team.TeamHeadPO;
import com.cslg.ac.demo.dao.user.po.team.TeamInfoPO;
import com.cslg.ac.demo.dao.user.po.team.TeamProfilePO;
import com.cslg.ac.demo.dao.user.team.TeamHeadDAO;
import com.cslg.ac.demo.dao.user.team.TeamInfoDAO;
import com.cslg.ac.demo.dao.user.team.TeamProfileDAO;
import com.cslg.ac.demo.entity.user.team.TeamHead;
import com.cslg.ac.demo.entity.user.team.TeamInfo;
import com.cslg.ac.demo.entity.user.team.TeamProfile;
import com.cslg.ac.demo.util.communicate.CosClientUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@PropertySource(value = {"classpath:user.properties"},encoding="utf-8")
public class TeamInfoService {

    @Autowired
    TeamInfoDAO teamInfoDAO;
    @Autowired
    TeamProfileDAO teamProfileDAO;
    @Autowired
    TeamHeadDAO teamHeadDAO;
    @Autowired
    LongAndLatDAO longAndLatDAO;
    @Value("${teamBuket}")
    String teamBuket;



    public List<Integer> getTeam(int offset, int num){
        return teamInfoDAO.selectTeam(offset, num);
    }

    public int updateLongAndLat(int id, double longitude, double latitude){
        if(longAndLatDAO.select(id) != null){
            return ResponseCode.FILE_HAVE_UPLOAD;
        }
        longAndLatDAO.insert(id, longitude, latitude);
        return ResponseCode.REQUEST_SUCCESSED;
    }

    public String getTable(){
        return CosClientUtil.GenerateSimplePresignedDownloadUrl("petTable.docx",teamBuket);
    }

    public int addInfo(TeamInfo teamInfo,TeamHead teamHead){
        TeamInfoPO teamInfoPO = new TeamInfoPO();
        TeamHeadPO teamHeadPO = new TeamHeadPO();
        BeanUtils.copyProperties(teamInfo,teamInfoPO);
        BeanUtils.copyProperties(teamHead,teamHeadPO);
        teamInfoDAO.updateBasicById(teamInfoPO);
        teamHeadDAO.updateChange(teamHeadPO);
        return ResponseCode.REQUEST_SUCCESSED;
    }

    public int changeInfo(TeamInfo teamInfo, TeamHead teamHead){
        TeamInfoPO teamInfoPO = new TeamInfoPO();
        TeamHeadPO teamHeadPO = new TeamHeadPO();
        BeanUtils.copyProperties(teamHead,teamHeadPO);
        BeanUtils.copyProperties(teamInfo,teamInfoPO);
        teamInfoDAO.updateChange(teamInfoPO);
        teamHeadDAO.updateChange(teamHeadPO);
        return ResponseCode.REQUEST_SUCCESSED;

    }

    public int changeProfile(TeamProfile teamProfile){
        TeamProfilePO teamProfilePO = new TeamProfilePO();
        BeanUtils.copyProperties(teamProfile,teamProfilePO);
        teamProfileDAO.updateBasicById(teamProfilePO);
        return ResponseCode.REQUEST_SUCCESSED;

    }

    public TeamInfo getBasicInfo(int id){
        TeamInfoPO teamInfoPO =  teamInfoDAO.selectById(id);
        TeamInfo teamInfo = new TeamInfo();
        BeanUtils.copyProperties(teamInfoPO,teamInfo);
        return teamInfo;
    }

    public TeamHead getHead(int id){
        TeamHeadPO teamHeadPO =  teamHeadDAO.selectById(id);
        TeamHead teamHead = new TeamHead();
        BeanUtils.copyProperties(teamHeadPO,teamHead);
        return teamHead;
    }

    public TeamProfile getProfile(int id){
        TeamProfilePO teamProfilePO =  teamProfileDAO.selectById(id);
        TeamProfile teamProfile = new TeamProfile();
        BeanUtils.copyProperties(teamProfilePO,teamProfile);
        return teamProfile;
    }


}
